package com.moon;

import me.leolin.shortcutbadger.ShortcutBadger;
import io.rong.imkit.RongIM;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sys.DemoApp;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.entity.UMessage;
import com.util.BadgeUtil;
import com.util.PreferencesUtils;

public class NotifyMessageHandler extends UmengMessageHandler {
	@Override
	public Notification getNotification(Context context,
	        UMessage msg) {
        //定义notification  
		JSONObject member = JSON.parseObject(PreferencesUtils.getString(context, "member"));
//		int mSilent = member.getIntValue("is_silent");
		
        Notification n=new Notification(R.drawable.ic_launcher,"月亮巴巴提示",System.currentTimeMillis());  
        /*if(mSilent == 1) */n.flags=Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;  
        /*else 
        {
        	n.flags = Notification.FLAG_AUTO_CANCEL;  
        	n.sound = Uri.parse("android.resource://" + "/null");
        }*/
        //通知消息与Intent关联  
        
        if(msg.text.contains("作业")) ((DemoApp)context.getApplicationContext()).mTab = 1;
        else if(msg.text.contains("通知")) ((DemoApp)context.getApplicationContext()).mTab = 2;
        else if(msg.text.contains("动态") || msg.text.contains("赞") || msg.text.contains("评论"))
        	((DemoApp)context.getApplicationContext()).mTab = 0;
        else ((DemoApp)context.getApplicationContext()).mTab = 3;
        
        PreferencesUtils.putInt(context, "notify_flag", ((DemoApp)context.getApplicationContext()).mTab);
        
        Intent intent = new Intent(context,MainActivity.class);
        PendingIntent pi=PendingIntent.getActivity(context, 0, 
        		intent, PendingIntent.FLAG_UPDATE_CURRENT);  
        //具体的通知内容  
        n.setLatestEventInfo(context, "月亮巴巴提示", msg.text,pi);  
        
        try{
        	int total = RongIM.getInstance().getRongIMClient().getTotalUnreadCount();
        	if(msg.title != null && msg.title.length() > 0) total += Integer.valueOf(msg.title).intValue();
//        	BadgeUtil.setBadgeCount(context, total);
        	
        	if(!ShortcutBadger.applyCount(context, total)) BadgeUtil.setBadgeCount(context, total);
        }
		catch(Exception e){}
        
        return n;
	}
}
